
try:
    import cPickle as pickle
except ImportError:
    import pickle

DEBUG = True

def dump_model(model, datafile):
    "dump a model to a file"
    file =  open(datafile, 'wb')
    pickle.dump(model, file, pickle.HIGHEST_PROTOCOL)
    pass

def read_model(datafile):
    "read a model from a file"
    global DEBUG
    if DEBUG: print "opening pickle file", datafile
    file =  open(datafile, 'rb')
    if DEBUG: print "unpickling..."
    model = pickle.load(file)
    if DEBUG: print "done."
    return model